home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Misc / BitmapTest / Source / TestView.m < prev   
Encoding:
Text File  |  1995-06-12  |  8.1 KB  |  228 lines

  1. /***************************************************************************/
  2. /* TestView.m - implementation file for TestView class                     */
  3. /* January 1990 Carl F. Sutter                               */
  4. /* This view is a simple animated bitmap test to see how fast they can     */
  5. /* move around.  The bitmaps are handled by the MovingBitmap class         */
  6. /***************************************************************************/
  7.  
  8. #import "TestView.h"
  9. #import <stdio.h>        // for fprintf REMOVE THIS AFTER DEBUGGING
  10. #import <string.h>        // for sprintf
  11. #import <dpsclient/psops.h>    // for PSsetgray
  12. #import <appkit/Control.h>    // for setIntValue
  13. #import <appkit/Cell.h>      // for setEnabled
  14. #import <appkit/nextstd.h>    // for MIN and MAX
  15.  
  16. @implementation TestView
  17.  
  18. /***************************************************************************/
  19. /* newFrame: - initialize the new view for the bitmaps to bounce around in */
  20. /***************************************************************************/
  21. - initFrame:(NXRect *)rect
  22.    {
  23.    [super initFrame:rect];
  24.    listMB = [[List alloc] init];            // new list to hold MovingBitmaps
  25.    bMoving = NO;
  26.    bGState = NO;
  27.    bPing = NO;
  28.    bEraseBackground = NO;
  29.    bEraseBitmaps = YES;
  30.    bTightDrawLoop = NO;
  31.    bRandomVelocity = YES;
  32.    dPeriod = 0.0;
  33.    timer = [[Animator alloc] initChronon:dPeriod adaptation:0.0 target:self
  34.                      action:@selector(step:) autoStart:NO eventMask:NX_ALLEVENTS];
  35.    return( self );
  36.    } /* newFrame: 1/25/89 CFS */
  37.  
  38.  
  39. /***************************************************************************/
  40. // appDidInit - Delegate message sent after application is set up.
  41. // The Views's window instance variable is now set, so it's size can be 
  42. // found.  Now find the minimum window size for a view of 1x1.
  43. /***************************************************************************/
  44. - appDidInit:(id)sender
  45.    {
  46.    NXRect    nxrWinFrame;
  47.   
  48.    [window getFrame:&nxrWinFrame];    
  49.    nxsMinWindow.width = nxrWinFrame.size.width; /* controls cross whole bottom */
  50.    nxsMinWindow.height = nxrWinFrame.size.height - bounds.size.height + 1.0;
  51.    return( self );
  52.    } /* appDidInit 8/16/89 CFS */
  53.    
  54.  
  55. /***************************************************************************/
  56. // windowWillResize - Delegate message sent from main window.
  57. // Prevent window from getting so small that the view size is zero.
  58. /***************************************************************************/
  59. - windowWillResize:sender toSize:(NXSize *)frameSize
  60.    {
  61.    frameSize->width  = MAX( frameSize->width,  nxsMinWindow.width );
  62.    frameSize->height = MAX( frameSize->height, nxsMinWindow.height );
  63.    return( self );
  64.    } /* windowWillResize 9/14/89 CFS */
  65.    
  66.  
  67. /***************************************************************************/
  68. // windowDidResize - Delegate message sent from main window.
  69. // After sizing, reset the limits for the moving bitmaps
  70. /***************************************************************************/
  71. - windowDidResize:sender;
  72.    {
  73.    [listMB makeObjectsPerform:@selector( setFrame: ) with:(id)&bounds];
  74.    return( self );
  75.    } /* windowDidResize 9/14/89 CFS */
  76.    
  77.  
  78. /***************************************************************************/
  79. /* setPeriod: - changes the period of the timer object               */
  80. /***************************************************************************/
  81. - setPeriod:sender
  82.    {
  83.    dPeriod = [timerPeriod doubleValue];
  84.    
  85.    [timer free];
  86.    timer = [[Animator alloc] initChronon:dPeriod adaptation:dPeriod target:self
  87.                      action:@selector(step:) autoStart:bMoving eventMask:NX_ALLEVENTS];
  88.    return( self );
  89.    } /* setPeriod: 1/25/90 CFS */
  90.  
  91.  
  92. /***************************************************************************/
  93. /* start: - start or stop the timer, and thus the animation               */
  94. /***************************************************************************/
  95. - start:sender
  96.    {
  97.    bMoving = !bMoving;
  98.    if (bMoving) [timer startEntry];
  99.    else [timer stopEntry];
  100.    return( self );
  101.    } /* start: 1/25/90 CFS */
  102.    
  103.    
  104. /***************************************************************************/
  105. /* step: - step the animation once, called from the timer object           */
  106. /***************************************************************************/
  107. - step:sender
  108.    {
  109.    if (bTightDrawLoop)
  110.       { /* keep drawing until an event happens - disregards timer delay! */
  111.       [self lockFocus];
  112.       while (![timer shouldBreak])
  113.          {
  114.          [listMB makeObjectsPerform:@selector( move )];
  115.      [self drawBitmaps];
  116.      [window flushWindow];
  117.      }
  118.       [self unlockFocus];
  119.       }
  120.    else /* just draw once */
  121.       {   
  122.       [listMB makeObjectsPerform:@selector( move )];
  123.       [self display];
  124.       }
  125.    return( self );
  126.    } /* step: 1/25/90 CFS */
  127.   
  128.    
  129. /***************************************************************************/
  130. /* create: - make a new moving bitmap, and add it to the list            */
  131. /***************************************************************************/
  132. - create:sender
  133.    {
  134.    MovingBitmap    *mbNew;
  135.    char            szTiffFile[40];
  136.    int            nSize;
  137.    
  138.    nSize = [[size selectedCell] tag];
  139.    sprintf( szTiffFile, "%dx%d.tiff", nSize, nSize );
  140.  
  141.    mbNew = [[MovingBitmap alloc] initFromMachO:szTiffFile inFrame:&bounds];
  142.    if (!bRandomVelocity)
  143.       [mbNew setVelocity:[xVel floatValue] :[yVel floatValue]];
  144.    [listMB addObject:mbNew];
  145.    
  146.    [count setIntValue:[listMB count]];
  147.    [self display];
  148.    return( self );
  149.    } /* create: 1/25/90 CFS */
  150.    
  151.    
  152. /***************************************************************************/
  153. /* removeOne: - remove the last bitmap on the list - the last one created  */
  154. /***************************************************************************/
  155. - removeOne:sender
  156.    {
  157.    BOOL     bSave = bEraseBackground;
  158.    
  159.    [listMB removeLastObject];
  160.    [count setIntValue:[listMB count]];
  161.    bEraseBackground = TRUE;
  162.    [self display];
  163.    bEraseBackground = bSave;
  164.    return( self );
  165.    } /* removeOne: 1/25/90 CFS */
  166.    
  167.  
  168. /***************************************************************************/
  169. /* allocateGState: - toggle GState allocation to this view               */
  170. /***************************************************************************/
  171. - allocateGState:sender
  172.    {
  173.    bGState = !bGState;
  174.    if (bGState) [self allocateGState];    // For faster lock/unlockFocus
  175.    else [self freeGState];
  176.    return( self );
  177.    } /* allocateGState: 1/25/90 CFS */
  178.    
  179.  
  180. /***************************************************************************/
  181. /* Boolean toggling action methods                               */
  182. /***************************************************************************/
  183. - nxPing:sender          {bPing = !bPing;                      return( self );}
  184. - eraseBackground:sender {bEraseBackground = !bEraseBackground;return( self );}
  185. - eraseBitmaps:sender    {bEraseBitmaps = !bEraseBitmaps;      return( self );}
  186. - tightDrawLoop:sender   {bTightDrawLoop = !bTightDrawLoop;    return( self );}
  187.  
  188.    
  189. /***************************************************************************/
  190. /* randomVel: - toggles random velocity for new bitmaps & enables fields   */
  191. /***************************************************************************/
  192.  - randomVel:sender;
  193.    {
  194.    bRandomVelocity = !bRandomVelocity;
  195.    [xVel setEnabled:!bRandomVelocity];
  196.    [yVel setEnabled:!bRandomVelocity];
  197.    return( self );
  198.    } /* randomVel: 1/25/90 CFS */
  199.    
  200.  
  201. /***************************************************************************/
  202. /* drawBitmaps - draw the bitmaps                               */
  203. /***************************************************************************/
  204. - drawBitmaps
  205.    {
  206.    if (bEraseBackground)
  207.       {
  208.       PSsetgray( NX_LTGRAY );
  209.       NXRectFill( &bounds );
  210.       }
  211.    if (bEraseBitmaps) [listMB makeObjectsPerform:@selector( erase )];
  212.    [listMB makeObjectsPerform:@selector( draw )];
  213.    if (bPing) NXPing();
  214.    } /* drawBitmaps 1/25/90 CFS */
  215.    
  216.  
  217. /***************************************************************************/
  218. /* drawSelf:: - draw the current view                           */
  219. /***************************************************************************/
  220. - drawSelf:(NXRect *)r :(int) count
  221.    {
  222.    [self drawBitmaps];
  223.    return( self );
  224.    } /* drawSelf:: 1/25/90 CFS */
  225.  
  226.  
  227. @end
  228.